package com.cxd.web.frontend;

import com.cxd.entity.EbNews;
import com.cxd.entity.EbProduct;
import com.cxd.entity.EbProductCategory;
import com.cxd.service.IEbNewsService;
import com.cxd.service.IEbProductCategoryService;
import com.cxd.service.IEbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/f-index")
public class EbIndexController {
    @Autowired
    private IEbProductService iEbProductService;
    @Autowired
    private IEbProductCategoryService iEbProductCategoryService;
    @Autowired
    private IEbNewsService iEbNewsService;


    //跳转到前台首页
    @RequestMapping("/index")
    public String front(HttpSession session,Model model,
                        @RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "pageSize", defaultValue = "8") int pageSize) {
        Object userNow = session.getAttribute("lgnUser");
        model.addAttribute("userNow",userNow);
        Object saved = session.getAttribute("productCategoryList");
        if(saved!=null){
            model.addAttribute("productCategoryList",saved);
        }else {
//        获得产品分类数据
            List<EbProductCategory> ebProductCategories = iEbProductCategoryService.listByPage();
            List<EbProductCategory> parentCategory = new ArrayList<>();
            List<EbProductCategory> orderlyCategory = new ArrayList<>();
            for (int i = 0; i < ebProductCategories.size(); ++i) {
                if (ebProductCategories.get(i).getEpcId() == ebProductCategories.get(i).getEpcParentId()) {
                    parentCategory.add(ebProductCategories.get(i));
                    orderlyCategory.add(ebProductCategories.get(i));
                    for (int m = 0; m < ebProductCategories.size(); ++m) {
                        if (ebProductCategories.get(i).getEpcId() == ebProductCategories.get(m).getEpcParentId()
                                && ebProductCategories.get(m).getEpcId() != ebProductCategories.get(m).getEpcParentId()) {
                            orderlyCategory.add(ebProductCategories.get(m));
                        }
                    }
                }
            }
            model.addAttribute("productCategoryList", orderlyCategory);
            session.setAttribute("productCategoryList",orderlyCategory);
        }

//        获得总产品列表
        List<EbProduct> productList = iEbProductService.listByPage(page, pageSize);
        final Integer totalPage = iEbProductService.findTotalPage(pageSize);

//      获取新闻列表
        Object newsList = session.getAttribute("ebNewsList");
        if(newsList==null){

            List<EbNews> ebNews = iEbNewsService.listByPage(1,20);
            model.addAttribute("ebNewsList",ebNews);
            session.setAttribute("ebNewsList",ebNews);
        }else {
            model.addAttribute("ebNewsList",newsList);
        }

        model.addAttribute("productList", productList);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currPage", page);

//        返回主页
        return "index";
    }
    //根据分类搜索
    @RequestMapping("/category")
    public String front(HttpSession session,Model model,int id,
                        @RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "pageSize", defaultValue = "8") int pageSize){
        Object userNow = session.getAttribute("lgnUser");
        model.addAttribute("userNow",userNow);
        Object saved = session.getAttribute("productCategoryList");
        model.addAttribute("productCategoryList",saved);
        Object newslist = session.getAttribute("ebNewsList");
        model.addAttribute("ebNewsList",newslist);
        List<EbProduct> productList = iEbProductService.listByCategory(id,page,pageSize);
        List<EbProduct> outlist = new ArrayList<>();
        for (int i = (page - 1) * pageSize;i < productList.size() && i < page * pageSize;++i){
            outlist.add(productList.get(i));
        }
        int totalPage = productList.size()/pageSize;
        if(productList.size()%pageSize!=0){
            ++totalPage;
        }
        model.addAttribute("productList", outlist);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currPage", page);
        return "index";
    }

}
